package com.qububu.play.dineTogether.service.impl;

import java.util.List;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.stereotype.Service;

import com.qububu.db.dao.DineDao;
import com.qububu.play.dineTogether.dto.ActionInfoDto;
import com.qububu.play.dineTogether.service.DineService;
import com.qububu.utils.DateUtil;

@Service
public class DineServiceImpl implements DineService {
	
	@Resource
	private DineDao dineDao;
	
	@Resource
	private Cache dineCache;// 正在进行的actionInfo, 最近活动actionInfo, 参加人员列表peopleList, staffList, foodList
	
//	private final String goingActionInfoKey = "goingActionInfoKey";
//	private final String outdatedActionListKey = "outdatedActionListKey";
	
	public boolean doAddActionApply(ActionInfoDto actionInfoDto) {
		// 先判断是否有活动在进行,如果有则返回false
		if (!nowTimeHaveGoingAction()) {
			// 将数据加入数据库
			dineDao.doAddActionApply(actionInfoDto);
			// 将数据存入缓存
//			doSaveElementToCache(goingActionInfoKey, actionInfoDto);
			
			return true;
		}
		
		return false;
	}


	public boolean doModifyActionInfoById(ActionInfoDto actionInfo) {
		/*if (removeElementFromCache(outdatedActionListKey)) {
			dineDao.doModifyActionInfoById(actionInfo);
			return true;
		}
		return false;*/
		dineDao.doModifyActionInfoById(actionInfo);
		return true;
	}


	public List<ActionInfoDto> getActionInfoList() {
		// 先从缓存中查找, 如果有就直接取缓存中的数据,没有则查库并保存到缓存,将数据返回
		/*List<ActionInfoDto> actionList = (List<ActionInfoDto>) getElementFromCache(outdatedActionListKey);
		if (actionList == null) {
			actionList = dineDao.getActionInfoList();
			doSaveElementToCache(outdatedActionListKey, actionList);
		}*/
		
		List<ActionInfoDto> actionList = dineDao.getActionInfoList();
		return actionList;
	}

	public ActionInfoDto getOngoingOrNearestInfo() {
		int nowTime = DateUtil.convertCurrentDTTMtoInt();
		// 从缓存中查找
		/*ActionInfoDto actionInfo = (ActionInfoDto) getElementFromCache(goingActionInfoKey);
		if (actionInfo == null) {
			// 从数据库取
			actionInfo = dineDao.getOngoingOrNearestInfo(nowTime);
			// 存入缓存
			doSaveElementToCache(goingActionInfoKey, actionInfo);
		}*/
		
		ActionInfoDto actionInfoDto = dineDao.getOngoingOrNearestInfo(nowTime);
		
		return actionInfoDto;
	}

	private Object getElementFromCache(String key) {
		Element element = dineCache.get(key);
		Object obj = null;
		if (element != null) {
//			System.out.println("\n\n--------调试--[dine]--[从缓存中[取出]一个元素]-------\n\n");
			obj = element.getObjectValue();
		}
		
		return obj;
	}

	private void doSaveElementToCache(String key, Object obj) {
		Element actionInfoElement = dineCache.get(key);
		if (actionInfoElement == null) {
//			System.out.println("\n\n--------调试--[dine]--[从缓存中[存入]一个元素]-------\n\n");
			dineCache.put(new Element(key, obj));
		}
	}
	
	// 清除指定key的缓存数据
	private boolean removeElementFromCache(String key) {
		if (dineCache.get(key) != null) {
//			System.out.println("\n\n--------调试--[dine]--[从缓存中[移除]一个元素]-------\n\n");
			return dineCache.remove(key);	
		}
		return false;
	}
	
	private boolean nowTimeHaveGoingAction() {
		if (getOngoingOrNearestInfo() == null) {
			return false;
		}
		
		return true;
	}

}

